#include "StreamPullerInputSource.h"
#include "Util/logc.h"
#include "RtspClientStream.h"

namespace Media{

    StreamPullerInputSource::StreamPullerInputSource(SourceMediaType mType, const std::string& url):
    InputSource(NETWORK, mType, url, true),m_url(url){
        tracef("create  StreamPullerInputSource");
        auto creater = GetStreamCreater();
        if(creater){
            m_rtspPlayer = creater->CreateStreamPlayer(url, true);
            if(!m_rtspPlayer){
                errorf("CreateStreamPlayer fail");
            }
        }
    }

    StreamPullerInputSource::~StreamPullerInputSource(){
        tracef("~StreamPullerInputSource");
        Stop();
    }

    bool StreamPullerInputSource::OpenSource(){
        tracef("StreamPullerInputSource  OpenSource !!!!! ");
        if(m_rtspPlayer){
            m_rtspPlayer->Start([this](const FrameCommon::Frame::Ptr& frame){
//                tracef("incoming  frame");
                if(frame->isVideo()) {
                    InputPacket(frame);
                }
            }, nullptr);
            return true;
        }

        tracef("StreamPullerInputSource  OpenSource  fail !!!!!  url %s", m_url.c_str());
        return false;
    }

    bool StreamPullerInputSource::CloseSource(){
        if(m_rtspPlayer){
            m_rtspPlayer->Stop();
        }
        return true;
    }

}